package com.xiaouyudeguang.common.executor;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.xiaouyudeguang.common.entity.CodeBody;
import com.xiaouyudeguang.common.utils.PingYinUtils;
import com.xiaouyudeguang.common.utils.XMindUtils;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.LinkedHashMap;
import java.util.Map;

@Slf4j
public class XMindExecutor {

    public static void execute(CodeBody body, String file, String xpath, String module, String tableName, String comment) throws Exception {
        body.setModule(module);
        body.setTableName(tableName);
        body.setComment(comment);
        execute(body, new File(file), xpath);
    }

    public static void execute(CodeBody body, String file, String xpath) throws Exception {
        execute(body, new File(file), xpath);
    }

    public static void execute(CodeBody body, File file, String xpath) throws Exception {
        ArrayNode arrayNode = XMindUtils.parse(file, xpath.split("\\."));
        Map<String, String> columnMap = new LinkedHashMap<>();
        for (JsonNode jsonNode : arrayNode) {
            columnMap.put(jsonNode.get("content").asText(), PingYinUtils.transform(jsonNode.get("content").asText()));
        }
        body.setColumns(columnMap);
        body.execute();
    }

}
